package top.zlhy7.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.zlhy7.vo.HelloVo;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

/**
 * @author 任勇
 * @date 2021/12/22 14:42
 * @description 测试控制器
 */
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {
    @GetMapping("list")
    public List<HelloVo> list(HttpServletRequest request){
        log.info("timestamp:{}",request.getParameter("timestamp"));
        Enumeration<String> headerNames = request.getHeaderNames();
        log.info("httpHead:");
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            log.info("{}:{}",name,request.getHeader(name));
        }
        log.info("查询列表");
        List<HelloVo> list = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            list.add(new HelloVo(i,"test"+i,new Date()));
        }
        return list;
    }
    @GetMapping("{id:\\d+}")
    public HelloVo findById(@PathVariable Integer id){
        HelloVo helloVo = new HelloVo(id, "test" + id, new Date());
        log.info("查询详情 {}",helloVo);
        return helloVo;
    }
    @PostMapping
    public void save(@RequestBody HelloVo helloVo){
        log.info("新增 {}",helloVo);
    }
    @PutMapping
    public void update(@RequestBody HelloVo helloVo){
        log.info("修改 {}",helloVo);
    }
    @DeleteMapping("{id:\\d+}")
    public void delete(@PathVariable Integer id){
        log.info("删除 {}",id);
    }

    @PostMapping("formSubmit")
    public void formSubmit(HelloVo helloVo){
        log.info("表单提交 {}",helloVo);
    }
    @PostMapping("upload")
    public void upload(MultipartFile file){
        log.info("上传文件 {}",file.getOriginalFilename());
        File file1 = new File(System.getProperty("user.dir")+"/"+file.getOriginalFilename());
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @GetMapping("download")
    public ResponseEntity<byte[]> download(){
        byte[] bytes = null;
        try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("测试下载.txt")){
            bytes = IOUtils.toByteArray(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //设置消息响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION,"attachment;fileName=" + new String("测试下载.txt".getBytes(), StandardCharsets.ISO_8859_1));
        return new ResponseEntity<>(bytes,headers, HttpStatus.OK);
    }
}
